package com.integral.controller;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.integral.biz.IBankBiz;
import com.integral.biz.IBankTypeBiz;
import com.integral.entity.Bank;
import com.integral.entity.BankType;
import com.integral.entity.UserInfo;
import com.integral.service.BankService;
import com.integral.util.UploadImgUtil;
import com.integral.vo.BankTypeVo;

@Controller
@RequestMapping("/bankType")
public class BankTypeController {
	protected final Log logger = LogFactory.getLog(getClass());
	@Resource
	private IBankTypeBiz bankTypeBiz;
	@Resource
	private BankService bankService;
	
	/**
	 * 查询银行产品信息列表
	 * @param bankTypeVo
	 * @return
	 */
	@RequestMapping(value="/findBankType")
	@ResponseBody
	public Map<String, Object> findBankType(BankTypeVo bankTypeVo){
		//设置查询未删除的数据
		bankTypeVo.setDeleteFlag(0);
		List<BankTypeVo> list = bankTypeBiz.findBankType(bankTypeVo);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", bankTypeBiz.findBankTypeCount(bankTypeVo));
		map.put("rows", list);
		return map;
	}
	/**
	 * 新增银行产品信息
	 * @param bankTypeVo
	 * @return
	 */
	@RequestMapping(value="/addBankType")
	public String addBankType(HttpServletRequest request,BankTypeVo bankTypeVo){
		BankType bankType = new BankType();
		bankType.setBankCode(bankTypeVo.getBankCode());
		bankType.setBankTypeCode(bankTypeVo.getBankTypeCode());
		bankType.setBankTypeName(bankTypeVo.getBankTypeName());
		Bank bank = bankService.findByBankCode(bankTypeVo.getBankCode());
		if(null != bank){
			//获得每万分兑换金额
			BigDecimal money = bank.getInceptionUnit();
			//获得1积分兑换金额
			money = money.divide(new BigDecimal(10000));
			//获得回购价格(1积分兑换金额*所需积分)
			BigDecimal counterPurchase = money.multiply(new BigDecimal(bankTypeVo.getIntegralNo()));
			//金额四舍五入保留2位小数
			counterPurchase = counterPurchase.setScale(2, BigDecimal.ROUND_HALF_UP);
			//回购价格
			bankType.setCounterPurchase(counterPurchase);
		}
		
		bankType.setExchangeNo(bankTypeVo.getExchangeNo());
		bankType.setIntegralNo(bankTypeVo.getIntegralNo());
		//上传文件-二维码
		if(null != bankTypeVo.getImg()){
			String fileName = UploadImgUtil.dowloadFile(bankTypeVo.getImg(), request);
			if("false".equals(fileName)){
				bankType.setImgFileName(null);
			}else{
				bankType.setImgFileName(fileName);
			}
		}
		//上传文件-银行产品截图示范
		if(null != bankTypeVo.getUploadImg()){
			String fileName = UploadImgUtil.dowloadFile(bankTypeVo.getUploadImg(), request);
			if("false".equals(fileName)){
				bankType.setBankTypeImg(null);
			}else{
				bankType.setBankTypeImg(fileName);
			}
		}
		bankTypeBiz.addBankType(bankType);
		return "management/bankType";
	}
	/**
	 * 修改银行产品信息
	 * @param bankVo
	 * @return
	 */
	@RequestMapping(value="/updateBankType")
	@ResponseBody
	public String updateBankType(BankTypeVo bankTypeVo,HttpServletRequest request){
		String msg = null;
		try {
			//获取登录对象
			HttpSession session = request.getSession();
			UserInfo user = (UserInfo)session.getAttribute("userInfo");
			
			BankType bankType = new BankType();
			//ID
			bankType.setId(bankTypeVo.getId());
			//所属银行
			bankType.setBankCode(bankTypeVo.getBankCode());
			//银行产品编码
			bankType.setBankTypeCode(bankTypeVo.getBankTypeCode());
			//银行产品名称
			bankType.setBankTypeName(bankTypeVo.getBankTypeName());
			Bank bank = bankService.findByBankCode(bankTypeVo.getBankCode());
			if(null != bank){
				//获得每万分兑换金额
				BigDecimal money = bank.getInceptionUnit();
				//获得1积分兑换金额
				money = money.divide(new BigDecimal(10000));
				//获得回购价格(1积分兑换金额*所需积分)
				BigDecimal counterPurchase = money.multiply(new BigDecimal(bankTypeVo.getIntegralNo()));
				//金额四舍五入保留2位小数
				counterPurchase = counterPurchase.setScale(2, BigDecimal.ROUND_HALF_UP);
				//回购价格
				bankType.setCounterPurchase(counterPurchase);
			}
			//兑换次数
			bankType.setExchangeNo(bankTypeVo.getExchangeNo());
			//所需积分
			bankType.setIntegralNo(bankTypeVo.getIntegralNo());
			
			//二维码
			if(null != bankTypeVo.getImg()){
				String Img = UploadImgUtil.dowloadFile(bankTypeVo.getImg(), request);
				if("false".equals(Img)){
					bankType.setImgFileName(null);
				}else{
					bankType.setImgFileName(Img);
				}
			}
			//银行产品截图示范
			if(null != bankTypeVo.getUploadImg()){
				String fileName = UploadImgUtil.dowloadFile(bankTypeVo.getUploadImg(), request);
				if("false".equals(fileName)){
					bankType.setBankTypeImg(null);
				}else{
					bankType.setBankTypeImg(fileName);
				}
			}
			//设置修改人名
			bankType.setUpdateEmp(user.getUserName());
			bankType.setUpdateTime(new Date());
			bankTypeBiz.updateBankType(bankType);
			msg = "修改成功！";
		} catch (Exception e) {
			msg = "修改失败！";
			logger.error("修改银行信息异常",e);
		}
		return msg;
	}
	/**
	 * 删除银行产品信息
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delBankType")
	@ResponseBody
	public String delBankType(Integer id,HttpServletRequest request){
		String msg = null;
		try {
			//获取登录对象
			HttpSession session = request.getSession();
			UserInfo user = (UserInfo)session.getAttribute("userInfo");
			BankType bankType = new BankType();
			bankType.setId(id);
			bankType.setDeleteFlag(1);
			//设置修改人名
			bankType.setUpdateEmp(user.getUserName());
			bankType.setUpdateTime(new Date());
			bankTypeBiz.updateBankType(bankType);
			msg = "删除成功！";
		} catch (Exception e) {
			msg = "删除失败！";
			logger.error("删除银行信息异常",e);
		}
		return msg;
	}
}
